<?php

/**
 * content actions.
 *
 * @package    www.furr.sk
 * @subpackage content
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contentActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $this->forward404Unless($category = $request->getParameter('category'));
      $content = Doctrine::getTable('Content')->getByCategoryId($category);
      if ($content)
          $this->redirect('content/edit?id=' . $content->getId() . '&category=' . $category);
      else
          $this->redirect('content/edit?category=' . $category);
  }

  public function executeEdit(sfWebRequest $request)
  {
     $content = null;
     $this->forward404Unless($category = $request->getParameter('category'));
     if ($request->hasParameter('id'))
        $this->forward404Unless($content = Doctrine::getTable('Content')->find($request->getParameter('id')), sprintf('Object content does not exist (%s).', $request->getParameter('id')));
     $this->form = new ContentForm($content);
     $this->form->setDefault('category_id', $category);
     $this->languages = MyFunctions::getLanguages();
     $this->form->embedI18n($this->languages);
  }

  public function executeUpdate(sfWebRequest $request)
  {
     $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
     $content = null;
     if ($request->hasParameter('id'))
        $this->forward404Unless($content = Doctrine::getTable('Content')->find($request->getParameter('id')), sprintf('Object content does not exist (%s).', $request->getParameter('id')));
     $this->form = new ContentForm($content);
     $this->languages = MyFunctions::getLanguages();
     $this->form->embedI18n($this->languages);
     $post = $request->getPostParameter($this->form->getName());
     $this->form->bind($post);
     if ($this->form->isValid())
     {
         $content = $this->form->save();
         $this->redirect('category/index');
     }
     $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
     $request->checkCSRFProtection();
     $this->forward404Unless($content = Doctrine::getTable('Content')->find($request->getParameter('id')), sprintf('Object content does not exist (%s).', $request->getParameter('id')));
     $content->delete();
     $this->redirect('category/index');
  }
}
